package com.test.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ssm.dao.UserDAO;
import com.test.ssm.pojo.TableData;
import com.test.ssm.pojo.User;
import com.test.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 徒有琴
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Override
    public User doLogin(String username, String password) {
        User user = userDAO.getUserByName(username);
        if (user == null || !user.getPassword().equals(password)) {
            return null;
        }
        return user;
    }

    @Override
    public TableData getUserList(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userDAO.getUserList(name);
        PageInfo pageInfo = new PageInfo(list);
        return new TableData(pageInfo.getTotal(), list);
    }

    @Override
    public void addUser(User user) {
        userDAO.addUser(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userDAO.getUserById(id);
    }

    @Override
    public void updateUser(User user) {
        userDAO.updateUser(user);
    }

    @Override
    public void deleteUsers(Integer[] ids) {
        if (ids != null && ids.length > 0) {
            for (Integer id : ids) {
                userDAO.deleteUserById(id);
            }
        }
    }
}
